package com.example.controller;


import com.example.entity.StockInfo;
import com.example.service.StockInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test")
public class StockInfoController {

    Logger logger = LoggerFactory.getLogger(StockInfoController.class);

    @Autowired
    private StockInfoService stockInfoService;

    /**
     * 根据股票代码查询股票信息
     * @param stockcode
     * @return 股票信息
     * 注意@PathVariable注释：映射 URL 绑定的占位符通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:
     * URL 中的 {xxx} 占位符可以通过
     * 一般与@RequestMapping(method = RequestMethod.GET)一起使用
     * http://localhost:8888/test/GetStockInfo/300348
     */
    @RequestMapping(value = "GetStockInfo/{stockcode}", method = RequestMethod.GET)
    public String GetStockInfo(@PathVariable String stockcode){
        logger.info("日志测试！！！！！！！");
        logger.debug("stockcode=%s",stockcode);
        return stockInfoService.getStockInfo(stockcode).toString();
    }

    /**
     *
     * @param stockcode
     * @return
     * http://localhost:8888/test/delete?stockcode=3
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String stockcode){
        logger.debug("stockcode=%s",stockcode);
        int result = stockInfoService.deleteByCode(stockcode);

        if(result >= 1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    /**
     *
     * @param stockInfo
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(StockInfo stockInfo){
        logger.debug("stockInfo=%s",stockInfo);
        int result = stockInfoService.update(stockInfo);
        if(result >= 1){
            return "更新成功";
        }else{
            return "更新失败";
        }
    }

    /**
     *
     * @param stockInfo
     * @return
     */
    //
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public int insert(StockInfo stockInfo){
        logger.debug("stockInfo=%s",stockInfo);
        return stockInfoService.save(stockInfo);

    }

    /**
     *
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public List<StockInfo> ListStockInfo(){
        logger.debug("selectAll>>>>>begin");
        return stockInfoService.selectAll();
    }
}
